package com.hust.yuqian.tests;

/**
 * Created by mmutuyu on 14-1-12.
 */
/*
 *********************************************************************
 *
 *    Computes the PSNR between two images.
 *
 *********************************************************************
 */

public class PNSR {

    public static double log10(double x) {
        return Math.log(x)/Math.log(10);
    }

    public static void calculate (int nrows, int ncols, int [][] img1_b, int [][] img2_b) {

        double  peak, signal, noise, mse;

        signal = noise = peak = 0;
        for (int i=0; i<nrows; i++) {
            for (int j=0; j<ncols; j++) {
                signal += img1_b[i][j] * img1_b[i][j];
                noise += (img1_b[i][j] - img2_b[i][j]) * (img1_b[i][j] - img2_b[i][j]);
                if (peak < img1_b[i][j])
                    peak = img1_b[i][j];
            }
        }

        mse = noise/(nrows*ncols); // Mean square error
        System.out.println("MSE: " + mse);
        System.out.println("SNR: " + 10*log10(signal/noise));
        System.out.println("PSNR(max=255): " + (10*log10(255*255/mse)));
        System.out.println("PSNR(max=" + peak + "): " + 10*log10((peak*peak)/mse));
    }


}